package cn.mumzone.pos.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.mumzone.basic.base.BaseActivity;
import cn.mumzone.basic.loading.button.LoadingButton;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.IsFastClickUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.InventoryGoodsEntity;
import cn.mumzone.pos.presenter.InventoryResultPresenter;
import cn.mumzone.pos.presenter.contract.InventoryResultContract;
import cn.mumzone.pos.ui.adapter.ADA_InventoryGoods;

/**
 * Created by qiuzhenhuan on 2019/10/12.
 */

public class ACT_InventoryResult extends BaseActivity<InventoryResultPresenter> implements InventoryResultContract.View {

    private static final String KEY_INVENTORY_ID = "inventory_id";
    private static final String KEY_INVENTORY_REMARK = "inventory_remark";
    private static final String KEY_INVENTORY_GOODS = "inventory_goods";

    @BindView(R.id.rv_inventory_empty_goods)
    RecyclerView rvInventoryEmptyGoods;
    @BindView(R.id.ll_empty_zero_goods)
    LinearLayout llEmptyZeroGoods;

    @BindView(R.id.rv_inventory_goods)
    RecyclerView rvInventoryGoods;
    @BindView(R.id.ll_empty_goods)
    LinearLayout llEmptyGoods;

    @BindView(R.id.btn_confirm)
    LoadingButton btnConfirm;

    private List<InventoryGoodsEntity> inventoryGoodsEntities;
    private String inventoryId;
    private String remark;

    private List<InventoryGoodsEntity> emptyGoods = new ArrayList<>();
    private List<InventoryGoodsEntity> inventoryGoods = new ArrayList<>();
    private List<InventoryGoodsEntity> otherInventoryGoods = new ArrayList<>();


    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        inventoryGoodsEntities = (List<InventoryGoodsEntity>) extras.getSerializable(KEY_INVENTORY_GOODS);
        inventoryId = extras.getString(KEY_INVENTORY_ID);
        remark = extras.getString(KEY_INVENTORY_REMARK);
        initGoodsData();
    }

    private void initGoodsData() {
        for (InventoryGoodsEntity entity : inventoryGoodsEntities) {
            if (!StringUtils.isEmpty(entity.getGoodsBatchNumList().get(0).getGoodsBatchNumber())) {//批次商品

                InventoryGoodsEntity zeroEntity = new InventoryGoodsEntity(); //盘点为0对象
                zeroEntity.setGoodsId(entity.getGoodsId());
                zeroEntity.setGoodsName(entity.getGoodsName());
                zeroEntity.setGoodsSpec(entity.getGoodsSpec());
                zeroEntity.setGoodsBatchNumList(new ArrayList<InventoryGoodsEntity.GoodsBatchNumListBean>());

                InventoryGoodsEntity inventoryEntity = new InventoryGoodsEntity(); //盘盈盘亏对象
                inventoryEntity.setGoodsId(entity.getGoodsId());
                inventoryEntity.setGoodsName(entity.getGoodsName());
                inventoryEntity.setGoodsSpec(entity.getGoodsSpec());
                inventoryEntity.setGoodsBatchNumList(new ArrayList<InventoryGoodsEntity.GoodsBatchNumListBean>());
                for (InventoryGoodsEntity.GoodsBatchNumListBean batchEntity : entity.getGoodsBatchNumList()) {
                    if (BigDecimalUtil.getNumber(batchEntity.getGoodsNum()).equals(BigDecimalUtil.getNumber(batchEntity.getSkuNum()))
                            && Double.parseDouble(BigDecimalUtil.getNumber(batchEntity.getGoodsNum())) != 0) {//盘点未变化
                        InventoryGoodsEntity otherEntity = new InventoryGoodsEntity();
                        otherEntity.setGoodsId(entity.getGoodsId());
                        otherEntity.setGoodsName(entity.getGoodsName());
                        otherEntity.setGoodsSpec(entity.getGoodsSpec());
                        otherEntity.setGoodsBatchNumList(new ArrayList<InventoryGoodsEntity.GoodsBatchNumListBean>());
                        otherEntity.getGoodsBatchNumList().add(batchEntity);
                        otherInventoryGoods.add(otherEntity);
                    } else {
                        if (Double.parseDouble(BigDecimalUtil.getNumber(batchEntity.getGoodsNum())) == 0) {//盘点为0
                            zeroEntity.getGoodsBatchNumList().add(batchEntity);
                        } else {//盘盈盘亏
                            inventoryEntity.getGoodsBatchNumList().add(batchEntity);
                        }
                    }
                }
                if (!ListUtil.isListEmpty(zeroEntity.getGoodsBatchNumList())) {//有盘点为0的批次
                    emptyGoods.add(zeroEntity);
                }

                if (!ListUtil.isListEmpty(inventoryEntity.getGoodsBatchNumList())) {//有盘盈盘亏的批次
                    inventoryGoods.add(inventoryEntity);
                }

            } else {
                //非批次商品
                InventoryGoodsEntity.GoodsBatchNumListBean batchEntity = entity.getGoodsBatchNumList().get(0);

                if (BigDecimalUtil.getNumber(batchEntity.getGoodsNum()).equals(BigDecimalUtil.getNumber(batchEntity.getSkuNum()))
                        && Double.parseDouble(BigDecimalUtil.getNumber(batchEntity.getGoodsNum())) != 0) {
                    otherInventoryGoods.add(entity);
                } else {
                    if (Double.parseDouble(BigDecimalUtil.getNumber(batchEntity.getGoodsNum())) == 0) {
                        emptyGoods.add(entity);
                    } else {
                        inventoryGoods.add(entity);
                    }
                }


            }
        }
    }

    @Override
    protected void initEventAndData() {

        rvInventoryEmptyGoods.setLayoutManager(new LinearLayoutManager(mContext));
        ADA_InventoryGoods emptyAdapter = new ADA_InventoryGoods(mContext);
        emptyAdapter.setInventoryStatus("3");
        emptyAdapter.setDataList(emptyGoods);
        rvInventoryEmptyGoods.setAdapter(emptyAdapter);

        if (ListUtil.isListEmpty(emptyGoods)) {
            rvInventoryEmptyGoods.setVisibility(View.GONE);
            llEmptyZeroGoods.setVisibility(View.VISIBLE);
        } else {
            rvInventoryEmptyGoods.setVisibility(View.VISIBLE);
            llEmptyZeroGoods.setVisibility(View.GONE);
        }

        rvInventoryGoods.setLayoutManager(new LinearLayoutManager(mContext));
        ADA_InventoryGoods adapter = new ADA_InventoryGoods(mContext);
        adapter.setInventoryStatus("3");
        adapter.setDataList(inventoryGoods);
        rvInventoryGoods.setAdapter(adapter);

        if (ListUtil.isListEmpty(inventoryGoods)) {
            rvInventoryGoods.setVisibility(View.GONE);
            llEmptyGoods.setVisibility(View.VISIBLE);
        } else {
            rvInventoryGoods.setVisibility(View.VISIBLE);
            llEmptyGoods.setVisibility(View.GONE);
        }
    }


    @Override
    public void showInventorySuccess() {

        PosToast.INSTANCE.makeSuccessToast(mContext, "操作成功");
        setResult(RESULT_OK);
        finish();
    }

    @OnClick({R.id.btn_confirm})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm:
                if (IsFastClickUtil.isFastClick(view)) {
                    return;
                }
                btnConfirm.startLoading("正在提交...");
                List<InventoryGoodsEntity> goodsEntityList = new ArrayList<>();
                goodsEntityList.addAll(emptyGoods);
                goodsEntityList.addAll(inventoryGoods);
                goodsEntityList.addAll(otherInventoryGoods);
                mPresenter.sendInventoryResult(inventoryId, goodsEntityList, remark);
                break;
        }
    }

    public static void startAcForResult(Activity context, String inventoryId, List<InventoryGoodsEntity> goodsEntities, String remark, int requestCode) {
        Intent intent = new Intent(context, ACT_InventoryResult.class);
        intent.putExtra(KEY_INVENTORY_ID, inventoryId);
        intent.putExtra(KEY_INVENTORY_GOODS, (Serializable) goodsEntities);
        intent.putExtra(KEY_INVENTORY_REMARK, remark);
        context.startActivityForResult(intent, requestCode);
    }

    @Override
    protected int getLayout() {
        return R.layout.act_inventory_result;
    }

}
